package com.saunter.controller;

import com.saunter.model.Company;
import com.saunter.model.Theme;
import com.saunter.service.user.AdminService;
import com.saunter.utils.ConfigProperties;
import com.saunter.utils.FileUtils;
import com.saunter.utils.exception.ExceptionManager;
import com.saunter.utils.response.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@Api(tags = {"管理员设置相关"})
@RestController
@RequestMapping("/admin")
public class AdminSetController {
    @Autowired
    private AdminService adminService;
    @Autowired
    private ConfigProperties configProperties;
    @Resource
    private ExceptionManager exceptionManager;

    public boolean checkSecondPw(Integer admin_id, String second_passwd) {
        String res_checkpw = adminService.checkSecondPasswd(admin_id, second_passwd);
        if("1".equals(res_checkpw)) {
            return true;
        }else {
            throw exceptionManager.create(res_checkpw);
        }
    }

    @ApiOperation("获取用户默认密码")
    @GetMapping("/getUserDefaultPw")
    public Map<String, Object> getUserDefaultPw(@ApiParam(value = "二级密码", required = true) @RequestParam("second_passwd")String second_passwd, @ApiParam(hidden = true) @RequestParam("system_append_admin_id")Integer admin_id) {
        this.checkSecondPw(admin_id, second_passwd);
        return Response.success(adminService.getUserDefaultPasswd());
    }

    @ApiOperation(value = "设置用户默认密码", notes = "密码至少八位，且必须包含数字和字符")
    @PostMapping("/setUserDefaultPw")
    public Map<String, Object> setUserDefaultPw(@RequestParam("new_passwd")String user_default_pw, @ApiParam(value = "二级密码", required = true) @RequestParam("second_passwd")String second_passwd, @ApiParam(hidden = true) @RequestParam("system_append_admin_id")Integer admin_id) {
        this.checkSecondPw(admin_id, second_passwd);
        String res_set = adminService.setUserDefaultPw(user_default_pw);
        if("1".equals(res_set)) {
            return Response.success(adminService.getUserDefaultPasswd());
        }else {
            throw exceptionManager.create(res_set);
        }
    }

    @ApiOperation("获取网站cookie加密密钥")
    @GetMapping("/getEncryptKey")
    public Map<String, Object> getEncryptKey(@ApiParam(value = "二级密码", required = true) @RequestParam("second_passwd")String second_passwd, @ApiParam(hidden = true) @RequestParam("system_append_admin_id")Integer admin_id) {
        this.checkSecondPw(admin_id, second_passwd);
        return Response.success(adminService.getEncryptKey());
    }

    @ApiOperation(value = "设置网站cookie加密密钥", notes = "密码至少八位，且必须包含数字和字符")
    @PostMapping("/setEncryptKey")
    public Map<String, Object> setEncryptKey(HttpServletResponse response, @RequestParam("new_passwd")String user_default_pw, @ApiParam(value = "二级密码", required = true) @RequestParam("second_passwd")String second_passwd, @ApiParam(hidden = true) @RequestParam("system_append_admin_id")Integer admin_id) {
        this.checkSecondPw(admin_id, second_passwd);
        String res_set = adminService.setEncryptKey(user_default_pw);
        if("1".equals(res_set)) {
            adminService.removeCookie(response);
            return Response.success(adminService.getEncryptKey());
        }else {
            throw exceptionManager.create(res_set);
        }
    }

    @ApiOperation("编辑公司")
    @PostMapping("/editCompany")
    public Map<String, Object> editCompany(@ApiParam(value = "二级密码", required = true) @RequestParam("second_passwd")String second_passwd, @ApiParam(hidden = true) @RequestParam("system_append_admin_id")Integer admin_id,
                                           @RequestParam(value = "company_name", required = false)String company_name, @RequestParam(value = "company_desc", required = false)String company_desc, @RequestParam(value = "site_title", required = false)String site_title, @ApiParam("网站备案号") @RequestParam(value = "record_code", required = false)String record_code) {
        this.checkSecondPw(admin_id, second_passwd);
        if(null == company_name && null == company_desc && null == site_title && null == record_code) {
            throw exceptionManager.create("Prm004");
        }
        if(1 == adminService.editCompony(new Company(null, company_name, site_title, record_code, company_desc, null))) {
            return Response.success("修改成功");
        }else {
            throw exceptionManager.create("Sql002");
        }
    }

    @ApiOperation(value = "修改主题颜色", notes = "所有颜色值为十六进制的#ffffff格式")
    @PostMapping("/editTheme")
    public Map<String, Object> editTheme(@ApiParam(value = "二级密码", required = true) @RequestParam("second_passwd")String second_passwd, @ApiParam(hidden = true) @RequestParam("system_append_admin_id")Integer admin_id,
                                         @RequestParam(value = "titleBarBg", required = false)String titleBarBg, @RequestParam(value = "titleBarColor", required = false)String titleBarColor, @RequestParam(value = "titleBarHover", required = false)String titleBarHover, @ApiParam("标题栏强调色") @RequestParam(value = "titleBarStress", required = false)String titleBarStress,
                                         @RequestParam(value = "navBarBg", required = false)String navBarBg, @RequestParam(value = "navBarColor", required = false)String navBarColor, @RequestParam(value = "navBarHover", required = false)String navBarHover, @RequestParam(value = "navBarIcon", required = false)String navBarIcon) {
        this.checkSecondPw(admin_id, second_passwd);
        if (1 == adminService.editTheme(new Theme(null, null, titleBarBg, titleBarColor, titleBarHover, titleBarStress, navBarBg, navBarColor, navBarHover, navBarIcon))) {
            return Response.success("修改成功");
        } else {
            throw exceptionManager.create("Sql002");
        }
    }

    @ApiOperation("获取用户职位显示状态")
    @GetMapping("/getShowUserJob")
    public Map<String, Object> getShowUserJob() {
        return Response.success(adminService.isShowUserJob());
    }

    @ApiOperation("设置用户职位显示状态")
    @PostMapping("/setShowUserJob")
    public Map<String, Object> setShowUserJob(@ApiParam(value = "二级密码", required = true) @RequestParam("second_passwd")String second_passwd, @ApiParam(hidden = true) @RequestParam("system_append_admin_id")Integer admin_id, @RequestParam("show_user_job") boolean show_user_job) {
        this.checkSecondPw(admin_id, second_passwd);
        return Response.success(adminService.setShowUserJob(show_user_job));
    }

    @ApiOperation(value = "设置网站图标", notes = "图标只能为png格式，且不超过10kb")
    @PostMapping("/setFavicon")
    public Map<String, Object> setFavicon(@ApiParam(value = "二级密码", required = true) @RequestParam("second_passwd")String second_passwd, @ApiParam(hidden = true) @RequestParam("system_append_admin_id")Integer admin_id, @RequestParam("icon") MultipartFile icon_file) throws IOException {
        this.checkSecondPw(admin_id, second_passwd);
        String project_path = FileUtils.getProjectPath();
        if(null == project_path || "".equals(project_path)) {
            throw exceptionManager.create("File006");
        }
        if(icon_file.isEmpty()) {
            throw exceptionManager.create("File001");
        }
        if(!"image/png".equals(icon_file.getContentType())) {
            throw exceptionManager.create("File002");
        }
        if(10 * 1024 < icon_file.getSize()){
            throw exceptionManager.create("File003");
        }
        FileUtils.saveFile(icon_file, project_path + "/static/favicon.png");
        return Response.success("设置成功");
    }
}
